package lightcar;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JButton.*;
import javax.swing.plaf.basic.BasicArrowButton;
import javax.swing.event.*;

class MyPanel extends JPanel {

    public MyPanel() {
        setBorder(BorderFactory.createLineBorder(Color.black));
        setBackground(Color.WHITE);
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(700,700);
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);       
        LightCar.world.draw(g);
    }
}

public class MyFrame extends JFrame 
{
    public MyFrame()
    {
        super();
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        JButton playButton = new JButton("Play");
        playButton.setSize(10, 60);
        JButton pauseButton = new JButton("Pause");
        pauseButton.setSize(10, 60);
        JButton resetButton = new JButton("Reset");
        resetButton.setSize(10, 60);
        JToggleButton addLightButton = new JToggleButton("Add Light");
        addLightButton.setSize(10, 60);
        JButton addCarButton = new JButton("Add Car");
        final JComboBox CarBehaviors = new JComboBox();
        ////////////////for adding a Car
        final JTextField newX = new JTextField();
        newX.setSize(10,60);
        final JTextField newY = new JTextField();
        newY.setSize(10,60);
        final JTextField newDir = new JTextField();
        newDir.setSize(10,60);
        final JTextField newk11 = new JTextField();
        newk11.setSize(10,60);
        final JTextField newk12 = new JTextField();
        newk12.setSize(10,60);
        final JTextField newk21 = new JTextField();
        newk21.setSize(10,60);
        final JTextField newk22 = new JTextField();
        newk22.setSize(10,60);
        
        JLabel newXlabel = new JLabel("x: ");
        JLabel newYlabel = new JLabel("y: ");
        JLabel newDirlabel = new JLabel("Dir: ");
        JLabel newk11label = new JLabel("k11: ");
        JLabel newk12label = new JLabel("k12: ");
        JLabel newk21label = new JLabel("k21: ");
        JLabel newk22label = new JLabel("k22: ");
       
        ////////////////////////////////////////
        
        //Add drop down list of car behaviors
        CarBehaviors.addItem("Attraction");
        CarBehaviors.addItem("Repulsion");
        //CarBehaviors.addItem("Wandering Eye");
        //CarBehaviors.addItem("Adorer");
        CarBehaviors.setSelectedItem(CarBehaviors.getItemAt(0));
        
        
        JLabel filler_label1 = new JLabel();
        JLabel filler_label2 = new JLabel();
        JLabel filler_label3 = new JLabel();
        JLabel filler_label4 = new JLabel();
        
        JPanel buttonsPanel = new JPanel();
        buttonsPanel.setLayout(new GridLayout(11,2,10,10)); //second number is columns
        buttonsPanel.setSize(200,200);
        buttonsPanel.add(playButton);
        buttonsPanel.add(pauseButton);
        buttonsPanel.add(resetButton);
        buttonsPanel.add(addLightButton);
       // buttonsPanel.add(filler_label1);
       // buttonsPanel.add(filler_label2);
        ///////////////////////////////for adding car
        buttonsPanel.add(newXlabel);
        buttonsPanel.add(newX);
        buttonsPanel.add(newYlabel);
        buttonsPanel.add(newY);
        buttonsPanel.add(newDirlabel);
        buttonsPanel.add(newDir);
        buttonsPanel.add(newk11label);
        buttonsPanel.add(newk11);
        buttonsPanel.add(newk12label);
        buttonsPanel.add(newk12);
        buttonsPanel.add(newk21label);
        buttonsPanel.add(newk21);
        buttonsPanel.add(newk22label);
        buttonsPanel.add(newk22);
        //buttonsPanel.add(filler_label3);
        //buttonsPanel.add(filler_label4);
        JLabel behavior_label = new JLabel("Car Behavior:");
        buttonsPanel.add(behavior_label);
        buttonsPanel.add(CarBehaviors);
        
        buttonsPanel.add(addCarButton);
        ///////////////////////////////////////////////
        
        JPanel controlPanel = new JPanel();
        controlPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
        controlPanel.setSize(300,300);
        controlPanel.add(buttonsPanel);
        
        MyPanel drawingArea = new MyPanel();
        drawingArea.setLayout(new BorderLayout());
        drawingArea.setSize(700,700);
        add(controlPanel, BorderLayout.EAST);
        add(drawingArea, BorderLayout.WEST);
    
        drawingArea.addMouseListener(new MouseAdapter()
      {
            @Override
            public void mousePressed(MouseEvent e)
            {
                if(LightSource.getLight()){
                    LightSource newLight = new LightSource(getMousePosition());
                    LightCar.world.lights.add(newLight);
                    LightCar.mainFrame.repaint();
                }
           }
       });
       
     
        playButton.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent e){
               if(LightCar.input_debug) System.out.println("Play button was clicked.");
               LightCar.start();
            }
        });
        pauseButton.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent e){
               if(LightCar.input_debug) System.out.println("Pause button was clicked.");
               LightCar.pause();
            }
        });
        resetButton.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent e){
               if(LightCar.input_debug) System.out.println("Reset button was clicked.");
               LightCar.world.reset();
               LightCar.mainFrame.repaint();
            }
        });
        addLightButton.addActionListener(new ActionListener(){

              public void actionPerformed(ActionEvent e){
                 System.out.println("light Button was Clicked");             
                 LightSource.toggleLight();
              }
          });
    
      
        addCarButton.addActionListener(new ActionListener(){

             public void actionPerformed(ActionEvent e){
                 System.out.println("addCar Button was Clicked");  
                 int x, y;
                 double k11, k12, k21, k22;
                 double dir;
                               
                 if (newX.getText().equals(""))
                 {
                     x=300;
                     newX.setText(String.valueOf(x));
                 }
                 else
                     x=Integer.parseInt(newX.getText());
                 
                 if (newY.getText().equals(""))
                 {
                     y = 310;
                     newY.setText(String.valueOf(y));
                 }
                 else
                     y=Integer.parseInt(newY.getText());
                 
                 
                 if (newDir.getText().equals(""))
                 {
                     dir = 0;
                     newDir.setText(String.valueOf(dir));
                 }
                 else
                     dir=Double.parseDouble(newDir.getText());
                 
                 if (newk11.getText().equals(""))
                 {
                     k11 = 0;
                     newk11.setText(String.valueOf(k11));
                 }
                 else
                     k11=Double.parseDouble(newk11.getText());
                 
                 if (newk12.getText().equals(""))
                 {
                     k12 = 20;
                     newk12.setText(String.valueOf(k12));
                 }
                 else
                     k12=Double.parseDouble(newk12.getText());
                 
                 if (newk21.getText().equals(""))
                 {
                     k21 = 20;
                     newk21.setText(String.valueOf(k21));
                 }
                 else
                     k21=Double.parseDouble(newk21.getText());
                 
                 if (newk22.getText().equals(""))
                 {
                     k22 = 0;
                     newk22.setText(String.valueOf(k22));
                 }
                 else
                     k22=Double.parseDouble(newk22.getText());
                 
                 Car newCar = new Car(new lightcar.Point(x,y), dir, k11, k12, k21, k22);
                 LightCar.world.cars.add(newCar);
                 LightCar.mainFrame.repaint();
                 //LightCar.pause();
                 
              }
          });
          
         CarBehaviors.addActionListener(new ActionListener()
         {
             @Override
             public void actionPerformed(ActionEvent e)
             {
                 int index = CarBehaviors.getSelectedIndex();
                 int x, y;
                 double k11, k12, k21, k22;
                 double dir;
                 
                 if (newX.getText().equals(""))
                 {
                     x=300;
                     newX.setText(String.valueOf(x));
                 }
                 else
                     x=Integer.parseInt(newX.getText());
                 
                 if (newY.getText().equals(""))
                 {
                     y = 310;
                     newY.setText(String.valueOf(y));
                 }
                 else
                     y=Integer.parseInt(newY.getText());
                 
                 
                 if (newDir.getText().equals(""))
                 {
                     dir = 0;
                     newDir.setText(String.valueOf(dir));
                 }
                 else
                     dir=Double.parseDouble(newDir.getText());
                 
                 
                 if (index == 0)
                 {
                     k11 = 0;
                     k12 = 20;
                     k21 = 20;
                     k22 = 0;
                 }
                 else if (index == 1)
                 {
                     k11 = 20;
                     k12 = 0;
                     k21 = 0;
                     k22 = 20;
                 }
                 else if (index == 2)
                 {
                     k11 = 0;
                     k12 = 20;
                     k21 = 20;
                     k22 = 0;   
                 }
                 else if (index == 3)
                 {
                     k11 = 20;
                     k12 = 0;
                     k21 = 0;
                     k22 = 20;
                 }
                 else
                 {
                     k11 = 0;
                     k12 = 20;
                     k21 = 20;
                     k22 = 0;   
                 }
                 newk11.setText(String.valueOf(k11));
                 newk12.setText(String.valueOf(k12));
                 newk21.setText(String.valueOf(k21));
                 newk22.setText(String.valueOf(k22));
           }
         });
                 
        
    }
}
